'use strict'

const http = require('http');
const fs = require('fs');

function parseBodyHeaders(data){
    let headers = {}
    let dsplit = data.split('\r\n').filter(p => p.length > 0)

    let ind
    let k
    for(let d of dsplit) {
        ind = d.indexOf(':')

        if(ind <= 0) continue

        k = d.subString(ind+1).trim()
    }

    let cpd = headers['content-disposition']

    let clist = cpd.split('; ').slice(1)

    let name, filename
    for(let a of clist) {
        ind = a.indexOf('filename=')
        if(ind >= 0){
            filename = a.substring(ind+10, a.length - 1)
        }else{
            name = a.substring(6, a.length - 1)
        }
    }

    return {
        name,
        headers,
        filename
    }

}

/**
 * @param {Buffer} bodyData 原始body数据
 * @param {Object} reqHeaders request.headers
 */

function parseBody(bodyData,reqHeaders){
    //content-type: multipart/form-data;
    let ctype = reqHeaders['content-type']
    
    //拿到分界线
    let bdy = ctype.substring(ctype.indexOf('=')+1)

    //数据中用于分割文件的分界线
    bdy = `--${bdy}\r\n`

    let crlf_bdy = `\r\n--${bdy}`

    //
    let data_end_index = bodyData.indexOf(crlf_bdy, bdy.length)

    let header_end_index = bodyData.indexOf('\r\n\r\n',bdy.length)

    //拿数据，先拿header
    let header_data = bodyData.toString('utf8',
                                        bdy.length,
                                        header_end_index)

    let hd = parseBodyHeaders(header_data)

    let fileinfo = {
        start:header_end_index+4,
        end:data_end_index,
    }

    fileinfo.length = fileinfo.end - fileinfo.start

    
    return {
        name:hd.name,
        filename:hd.headers,
        headers:hd.headers,
        start:fileinfo.start,
        end:fileinfo.end,
        length:fileinfo.length
    }
}

let routerTable = {
    GET:{
        '/upload':async(request,response) => {
            //GET /upload HTTP/1.1
            let stm = fs.createReadStream("./upload.html")
            stm.pipe(response)
            stm.on("end",() => {
                response.end()
            })
        }
    },
    POST:{
        '/upload':async(request,response) => {

        console.log(request.headers['content-type'])
            //POST /upload HTTP/1.1
            let bufferList = []
            let totalLength = 0

            request.on("data", chunk => {
                totalLength += chunk.length
                bufferList.push(chunk);
            })

            let bodyData =  null

            request.on('end',() => {
                bodyData = Buffer.concat(bufferList,totalLength)
                bufferList = []

                let body = parseBody(bodyData,request.headers)
                console.log(body)

                response.setHeader('content-type','text/plain;charset:utf-8')

                response.end(bodyData)
            })


        }
    }
}
let serv = http.createServer()

serv.on('request',(request,response) => {
    let rm = routerTable[request.method]

    if(!rm || !rm[ request.url]){
        response.statusCode = 404
        response.end('page not found')
        return
    }

    rm[request.url](request,response)
})

serv.listen(3456)